﻿namespace RolePlayingGame
{
    using System;
    using System.Collections.Generic;
    using System.Threading;
    static class Logo
    {

        private static char[,] stackBody = new char[,]
            {
                {' ',' ',' ',' ','S','S','S',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                {' ',' ',' ','S','S','S',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                {' ',' ','S','S','S',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                {' ','S','S','S',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                {'S','S','S','S','S','S','S','S',' ',' ',' ','T','T',' ',' ',' ',' ',' ',' ',' ','A','A',' ',' ',' ',' ',' ',' ','C','C','C','C','C','C',' ',' ','K','K',' ',' ',' ','K'},
                {' ',' ',' ',' ','S','S','S',' ',' ',' ','T','T','T','T',' ',' ',' ',' ',' ','A','A','A','A',' ',' ',' ',' ',' ','C','C',' ',' ',' ',' ',' ',' ','K','K',' ',' ','K',' '},
                {' ',' ',' ','S','S','S',' ',' ',' ',' ',' ','T','T',' ',' ',' ',' ',' ','A','A',' ',' ','A','A',' ',' ',' ',' ','C','C',' ',' ',' ',' ',' ',' ','K','K','K','K',' ',' '},
                {' ',' ','S','S','S',' ',' ',' ',' ',' ',' ','T','T',' ',' ',' ',' ','A','A','A','A','A','A','A','A',' ',' ',' ','C','C',' ',' ',' ',' ',' ',' ','K','K',' ',' ','K',' '},
                {' ','S','S','S',' ',' ',' ',' ',' ',' ',' ','T','T','T',' ',' ','A','A',' ',' ',' ',' ',' ',' ','A','A',' ',' ','C','C','C','C','C','C',' ',' ','K','K',' ',' ',' ','K'}
            };

        private static List<char[,]> overflowBody = new List<char[,]>();

        private static char[,] letterO = new char[,]
        {
            { 'O','O','O','O','O'},
            { 'O',' ',' ',' ','O'},
            { 'O',' ',' ',' ','O'},
            { 'O','O','O','O','O'},
        };

        private static char[,] letterV = new char[,]
        {
            { 'V',' ',' ',' ',' ',' ','V'},
            { ' ','V',' ',' ',' ','V',' '},
            { ' ',' ','V',' ','V',' ',' '},
            { ' ',' ',' ','V',' ',' ',' '},
        };

        private static char[,] letterE = new char[,]
        {
            { 'E',' ','E',' ','E'},
            { 'E',' ',' ',' ',' '},
            { 'E',' ',' ',' ',' '},
            { 'E',' ','E',' ','E'},
        };

        private static char[,] letterR = new char[,]
        {
            { 'R',' ','R',' ','R'},
            { 'R',' ','R',' ','R'},
            { 'R',' ','r',' ',' '},
            { 'R',' ',' ','r',' '},
        };

        private static char[,] letterF = new char[,]
        {
            { 'F','F','F','F'},
            { 'F',' ',' ',' '},
            { 'F','F','F','F'},
            { 'F',' ',' ',' '},
        };

        private static char[,] letterL = new char[,]
        {
            { 'L',' ',' ',' '},
            { 'L',' ',' ',' '},
            { 'L',' ',' ',' '},
            { 'L','L','L','L'},
        };

        private static char[,] letterW = new char[,]
        {
            { 'W',' ',' ',' ',' ',' ','W'},
            { ' ','W',' ',' ',' ','W',' '},
            { ' ','W',' ','W',' ','W',' '},
            { ' ','W','_','W','_','W',' '},
        };


        public static void DisplayLogo()
        {
            Console.CursorVisible = false;
            PrintStack();
            FillOverflowBody();
            PrintOverflow();
            PrintPressEnter();
            ListenKey();
        }

        private static void ListenKey()
        {
            while (true)
            {
                var key = Console.ReadKey(true);

                if (key.Key.Equals(ConsoleKey.Enter))
                {
                    Console.Clear();
                    Menu.Start();
                    break;
                }
            }
        }

        private static void PrintPressEnter()
        {
            string press = "Press enter to continue";
            int startWidth = WidthFinalPosition(press.Length);
            int startHeight = HeightFinalPosition(1);
            startHeight += 10;
            Console.SetCursorPosition(startWidth, startHeight);
            Console.Write(press);
        }

        private static void PrintOverflow()
        {
            Console.ForegroundColor = ConsoleColor.DarkRed;
            int startWidth = WidthFinalPosition(37 + 21);
            int startHeight = (Console.WindowHeight / 2) + 2;
            Console.SetCursorPosition(startWidth, startHeight);
            foreach (var letter in overflowBody)
            {
                int height = startHeight;
                Console.SetCursorPosition(startWidth, startHeight);
                for (int i = 0; i < letter.GetLength(0); i++)
                {
                    for (int j = 0; j < letter.GetLength(1); j++)
                    {
                        Console.Write(letter[i, j]);
                        Thread.Sleep(5);
                    }
                    height++;
                    Console.SetCursorPosition(startWidth, height);
                }
                startWidth += (letter.GetLength(1) + 3);
            }
            Console.ResetColor();
        }

        private static void PrintStack()
        {
            int width = WidthFinalPosition(stackBody.GetLength(1));
            int finalHeight = HeightFinalPosition(stackBody.GetLength(0));
            int height = 0;
            while (finalHeight >= height)
            {
                Console.SetCursorPosition(width, height);
                DisplayStack(width, height);
                Thread.Sleep(50);
                if (finalHeight != height)
                {
                    Console.SetCursorPosition(width, height);
                    EraseStack(width, height);
                }
                height++;
            }
        }

        private static void DisplayStack(int width, int height)
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            for (int i = 0; i < stackBody.GetLength(0); i++)
            {
                for (int j = 0; j < stackBody.GetLength(1); j++)
                {
                    Console.Write(stackBody[i, j]);
                }
                height++;
                Console.SetCursorPosition(width, height);
            }
        }

        private static void EraseStack(int width, int height)
        {
            for (int i = 0; i < stackBody.GetLength(0); i++)
            {
                for (int j = 0; j < stackBody.GetLength(1); j++)
                {
                    Console.Write(' ');
                }
                height++;
                Console.SetCursorPosition(width, height);
            }
        }

        private static int WidthFinalPosition(int logoWidth)
        {
            int consoleMiddle = Console.WindowWidth / 2;
            return consoleMiddle - (logoWidth / 2);
        }

        private static int HeightFinalPosition(int logoHeight)
        {
            int consoleMiddle = Console.WindowHeight / 2;
            return consoleMiddle - logoHeight;
        }

        private static void FillOverflowBody()
        {
            overflowBody.Add(letterO);
            overflowBody.Add(letterV);
            overflowBody.Add(letterE);
            overflowBody.Add(letterR);
            overflowBody.Add(letterF);
            overflowBody.Add(letterL);
            overflowBody.Add(letterO);
            overflowBody.Add(letterW);
        }
    }
}
